package com.jetpack.jetpacknavtest;

import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.NavigationUI;

import com.jetpack.jetpacknavtest.databinding.ActivityMainBottomBinding;

public class MainActivity extends AppCompatActivity {

    ActivityMainBottomBinding mainBinding;
    View view;
    LifeObserver lifeObserver ;
    MyViewModel myViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main_bottom);

        lifeObserver = new LifeObserver();
        myViewModel = new ViewModelProvider(this,new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(MyViewModel.class);
        getLifecycle().addObserver(lifeObserver);
        FragmentManager fragmentManager = getSupportFragmentManager();
        NavHostFragment navHostFragment = (NavHostFragment) fragmentManager.findFragmentById(R.id.fragments);
        NavController navController = navHostFragment.getNavController();
        NavigationUI.setupWithNavController(mainBinding.navView, navController);


    }
    @Override
    public boolean onSupportNavigateUp() {
//        onSupportNavigateUp()方法的重写，意味着Activity将它的 back键点击事件的委托出去，如果当前并非栈中顶部的Fragment, 那么点击back键，返回上一个Fragment。
        return Navigation.findNavController(this,R.id.nav_view).navigateUp();
    }
}
